package com.xbongbong.paas.toolbox.wrap;

import lombok.ToString;

import java.io.Serializable;

/**
 * 定义给前端的一个参数返回模式
 * @author youli.chen
 * 修改时间：2018-7-30 14:25 by kaka
 * @param <T> 被传输VO对象
 */
@ToString
public class ResultVO<T> implements Serializable
{
    private static final long serialVersionUID = 1L;

    /**
     * 错误信息返回
     */
    protected String msg;
    /**
     * 错误码
     */
    protected Integer code;
    /**
     * 返回是否成功
     */
    private Boolean success;
    /**
     * 返回值核心内容
     */
    private T result;

    /**
     * 记录总数
     */
    private Integer totalCount;

    public ResultVO() {
        this.success = true;
        this.totalCount = 0;
    }

    public ResultVO(Boolean bool) {
        this.success = bool.booleanValue();
        this.totalCount = 0;
    }

    public T getResult() {
        return this.result;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public boolean isEmpty() {
        return !isNotEmpty();
    }

    private boolean isNotEmpty() {
        return (isSuccess()) && (getResult() != null);
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getTotalCount()
    {
        return this.totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * 设置错误信息
     * @param code 错误码
     * @param msg 错误提示信息
     */
    public void setErrorResultMessage(Integer code, String msg)
    {
        this.success = false;
        setMsg(msg);
        setCode(code);
    }
}